﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    ///  Type类型扩展方法类
    /// </summary>
    public static partial class TypeEx
    {

        /// <summary>
        /// 已创建过后类型[key:类型名称;value:Type]
        /// </summary>
        private static ConcurrentDictionary<string, Type> _typeDic = null;

        /// <summary>
        /// 根据类型全名称转换为类型
        /// </summary>
        /// <param name="assemblyQualifiedName">
        /// System.Type.AssemblyQualifiedName的扩展,以逗号分隔
        /// 第一部分为类型名称Type.FullName
        /// 第二部分为程序集路径,可为当前程序相对路径,也可为绝对路径,路径的目录最后一级如果为“*”号，则为查找目录以下子目，否则仅查指定的目录
        /// 第三部分为版本号,可省略,当省略时则不验证版本号
        /// 示例:
        /// Oracle.ManagedDataAccess.Client.OracleConnection,Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,DBPlugins\SqlServer\UtilZ.Dotnet.DBSqlServer, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,DBPlugins\SqlServer\UtilZ.Dotnet.DBSqlServer
        /// UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer
        /// UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,D:\DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer
        /// </param>
        /// <param name="hasCache">是否缓存目标的类型[true:缓存;false:不缓存]</param>
        public static Type GetType(string assemblyQualifiedName, bool hasCache = false)
        {
            if (string.IsNullOrWhiteSpace(assemblyQualifiedName))
            {
                throw new ArgumentNullException(nameof(assemblyQualifiedName));
            }

            Type type;
            if (hasCache)
            {
                if (_typeDic == null)
                {
                    _typeDic = new ConcurrentDictionary<string, Type>();
                }

                if (_typeDic.TryGetValue(assemblyQualifiedName, out type))
                {
                    return type;
                }
            }

            #region 获取类型
            string[] strArr = assemblyQualifiedName.Split(',');
            if (strArr.Length < 2)
            {
                throw new NotSupportedException($"不支持的格式\"{assemblyQualifiedName}\"");
            }

            string assemblyName = Path.GetFileName(strArr[1].Trim());//程序集名称
            Assembly assembly = AssemblyEx.FindAssembly(assemblyName);
            if (assembly == null)
            {
                int index = assemblyQualifiedName.IndexOf(',');
                string assemblyFullName = assemblyQualifiedName.Substring(index + 1).Trim();
                assembly = LoadAssemblyByName(assemblyFullName);
            }

            if (assembly == null)
            {
                return null;
            }

            type = assembly.GetType(strArr[0].Trim(), false, true);
            #endregion

            if (type != null && hasCache)
            {
                _typeDic.TryAdd(assemblyQualifiedName, type);
            }

            return type;
        }

        /// <summary>
        /// 加载程序集,成功返回Assembly,失败返回null
        /// </summary>
        /// <param name="assemblyFullName">
        /// 带路径的System.Reflection.Assembly.FullName扩展(eg:"ConAppCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
        /// 第一部分为程序集路径,可为当前程序相对路径,也可为绝对路径,路径的目录最后一级如果为“*”号，则为查找目录以下子目，否则仅查指定的目录
        /// 第二部分为版本号,可省略,当省略时则不验证版本号
        /// 示例:
        /// Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// DBPlugins\SqlServer\UtilZ.Dotnet.DBSqlServer, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342
        /// DBPlugins\SqlServer\UtilZ.Dotnet.DBSqlServer
        /// DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer
        /// D:\DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer
        /// </param>
        /// <returns>Assembly</returns>
        public static Assembly LoadAssemblyByName(string assemblyFullName)
        {
            if (string.IsNullOrWhiteSpace(assemblyFullName))
            {
                throw new ArgumentNullException(nameof(assemblyFullName));
            }

            int index = assemblyFullName.IndexOf(',');
            string assemblyPath, versionInfo = null;
            if (index > 0)
            {
                //有版本号
                assemblyPath = assemblyFullName.Substring(0, index);
                versionInfo = assemblyFullName.Substring(index + 1);
            }
            else
            {
                //无版本号
                assemblyPath = assemblyFullName;
            }

            string name = Path.GetFileName(assemblyPath);

            //true:查询指定目录及所有子目录;false:仅仅查询指定目录
            bool findArea = false;
            index = assemblyPath.IndexOf('*');
            if (index > 0)
            {
                findArea = true;
                string modDir = assemblyPath.Substring(0, index);
                assemblyPath = PathEx.Combine(PathType.Local, modDir, name);
            }

            if (string.IsNullOrEmpty(Path.GetPathRoot(assemblyPath)))
            {
                //如果根路径为空,则为相对路径,则修改为相对工作目录的全路径
                //注:此处没有使用Path.GetFullPath这个API的原因是,在web项目中,
                //asp.net会调用Directory.SetCurrentDirectory()修改工作目录为代码目录
                //虽然可以显示的通过调用Directory.GetCurrentDirectory()修改为应用程序目录,
                //但是终究又是一个坑,所以此处使用PathEx.GetFullPath()方法来获取完整路径
                //assemblyPath = Path.GetFullPath(assemblyPath);
                assemblyPath = PathEx.GetFullPath(assemblyPath);
            }

            //注:因为程序集文件名通常为点连接多段字符串,如果是获取扩展的话,则最后一段名称会被认为是扩展名
            Assembly assembly;
            if (name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
            {
                //有扩展名

                if (findArea)
                {
                    //查询指定目录及所有子目录
                    DirectoryInfo parentDir = Directory.GetParent(assemblyPath);
                    if (parentDir == null || !parentDir.Exists)
                    {
                        return null;
                    }

                    FileInfo[] files = parentDir.GetFiles(name, SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        assembly = PrimitiveLoadAssembly(file.FullName, versionInfo);
                        if (assembly != null)
                        {
                            return assembly;
                        }
                    }
                }
                else
                {
                    //仅仅查询指定目录
                    return PrimitiveLoadAssembly(assemblyPath, versionInfo);
                }
            }
            else
            {
                //无扩展名

                string[] extensionArr = new string[] { ".dll", ".exe" };
                string filePath;
                if (findArea)
                {
                    //查询指定目录及所有子目录
                    DirectoryInfo parentDir = Directory.GetParent(assemblyPath);
                    if (parentDir == null || !parentDir.Exists)
                    {
                        return null;
                    }

                    string serachParten;
                    foreach (var extension in extensionArr)
                    {
                        serachParten = $"{name}{extension}";
                        FileInfo[] files = parentDir.GetFiles(serachParten, SearchOption.AllDirectories);
                        foreach (var file in files)
                        {
                            assembly = PrimitiveLoadAssembly(file.FullName, versionInfo);
                            if (assembly != null)
                            {
                                return assembly;
                            }
                        }
                    }
                }
                else
                {
                    //仅仅查询指定目录
                    foreach (var extension in extensionArr)
                    {
                        filePath = $"{assemblyPath}{extension}";
                        assembly = PrimitiveLoadAssembly(filePath, versionInfo);
                        if (assembly != null)
                        {
                            return assembly;
                        }
                    }
                }
            }

            return null;
        }

        private static Assembly PrimitiveLoadAssembly(string assemblyPath, string versionInfo)
        {
            if (!File.Exists(assemblyPath))
            {
                return null;
            }

            try
            {
                if (!string.IsNullOrWhiteSpace(versionInfo))
                {
                    AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
                    string assemblyVer;
                    int index = assemblyName.FullName.IndexOf(',');
                    if (index > 0)
                    {
                        assemblyVer = assemblyName.FullName.Substring(index + 1).Trim();
                    }
                    else
                    {
                        assemblyVer = assemblyName.FullName;
                    }

                    if (!string.Equals(versionInfo, assemblyVer))
                    {
                        //版本信息不匹配且版本也不匹配,则无效
                        ZLog.ZLoger.Debug($"找到同名程序集\"{assemblyPath}\",其版本信息为\"{assemblyVer}\"与期望版本信息\"{versionInfo}\"不匹配,忽略该程序集.");
                        return null;
                    }
                }

                return Assembly.LoadFile(assemblyPath);
            }
            catch
            { }

            return null;
        }


        /// <summary>
        /// 获取类型对应的TypeCategorys
        /// </summary>
        /// <param name="type">目标类型</param>
        /// <returns>TypeCategorys</returns>
        public static TypeCategorys GetTypeTypeCategory(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            TypeCategorys category;
            Type targetType = Nullable.GetUnderlyingType(type);
            if (targetType == null)
            {
                targetType = type;
            }

            if (IsBasicType(targetType))
            {
                category = TypeCategorys.Basic;
            }
            else if (typeof(object) == type)
            {
                //object类型
                category = TypeCategorys.Any;
            }
            else
            {
                TypeCode typeCode = Type.GetTypeCode(targetType);
                if (targetType.GetInterface(typeof(IDictionary).FullName) != null)
                {
                    category = TypeCategorys.IDictionary;
                }
                else if (targetType.GetInterface(typeof(IList).FullName) != null)
                {
                    category = TypeCategorys.IList;
                }
                else
                {
                    category = TypeCategorys.Object;
                }
            }

            return category;
        }


        private static bool IsBasicType(Type targetType)
        {
            if (targetType.IsPrimitive ||
            targetType.IsEnum ||
            targetType == typeof(DateTimeOffset) ||
            targetType == typeof(string) ||
            targetType == typeof(DateTime) ||
            targetType == typeof(decimal))
            {
                //基元类型: Boolean、 Byte、 SByte、 Int16、 UInt16、 Int32、 UInt32、 Int64、 UInt64、 IntPtr、 UIntPtr、 Char、 Double 和 Single。
                //系统基元类型、枚举
                return true;
            }

#if CORE
            if (targetType == typeof(DateOnly) ||
                targetType == typeof(TimeOnly))
            {
                return true;
            }
#else
#endif

            return false;
        }

        /// <summary>
        /// 获取实现了IList接口集合中元素类型,不包括IDictionary
        /// </summary>
        /// <param name="type">IList集合类型</param>
        /// <returns>元素类型</returns>
        /// <exception cref="NotImplementedException"></exception>
        public static Type GetIListElementType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.GetInterface(typeof(IList).FullName) == null)
            {
                throw new ArgumentException($"类型:{type.FullName}需要实现{typeof(IList).FullName}接口");
            }

            Type eleType;
            if (type.IsArray)
            {
                string eleTypeFullName = type.FullName.Substring(0, type.FullName.Length - 2);
                eleType = type.Assembly.GetType(eleTypeFullName);
            }
            else if (type.IsGenericType)
            {
                eleType = type.GetGenericArguments().First();
            }
            else
            {
                throw new NotImplementedException(type.FullName);
            }

            return eleType;
        }

        /// <summary>
        /// 获取泛型类型名称
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>类型名称</returns>
        public static string GetGenericTypeName(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            string typeName;
            const char _GENERIC_TYIPE_NAME_SPLIT = '`';
            int index = type.Name.IndexOf(_GENERIC_TYIPE_NAME_SPLIT);
            if (index < 0)
            {
                typeName = type.Name;
            }
            else
            {
                typeName = type.Name.Substring(0, index);
            }

            return typeName;
        }

        /// <summary>
        /// 获取实现了IDictionary接口集合中元素类型
        /// </summary>
        /// <param name="type">IDictionary集合类型</param>
        /// <returns>元素类型</returns>
        /// <exception cref="NotImplementedException"></exception>
        public static IDictionaryKeyValueType GetIDictionaryKeyValueType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.GetInterface(typeof(IDictionary).FullName) == null)
            {
                throw new ArgumentException($"类型:{type.FullName}需要实现{typeof(IDictionary).FullName}接口");
            }

            Type[] genericArguments = type.GetGenericArguments();
            return new IDictionaryKeyValueType(genericArguments[0], genericArguments[1]);
        }

        /// <summary>
        /// 获取类型的基元类型
        /// eg:Int32? => Int32
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>基元类型</returns>
        public static Type GetPrimitiveType(Type type)
        {
            Type underlyingType = Nullable.GetUnderlyingType(type);
            if (underlyingType != null)
            {
                return underlyingType;
            }
            else
            {
                return type;
            }
        }

        /// <summary>
        /// 确定当前的类型是继承自指定的接口[true:继承自接口;false:未继承自接口]
        /// </summary>
        /// <param name="type">当前的类型</param>
        /// <param name="interfaceType">接口类型</param>
        /// <returns>true:继承自接口;false:未继承自接口</returns>
        public static bool IsSubInterfaceOf(this Type type, Type interfaceType)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType));
            }

            return type.GetInterface(interfaceType.FullName) != null;
        }



#if NET4_0
        /// <summary>
        /// 在派生类中重写时，返回自定义特性的数组
        /// 异常:
        /// T:System.TypeLoadException:无法加载自定义特性类型。
        /// T:System.InvalidOperationException:该成员属于加载到仅反射上下文的类型。请参见How to: Load Assemblies into the Reflection-Only Context。
        /// </summary>
        /// <param name="memberInfo">目标成员</param>
        /// <param name="inherit">指定是否搜索该成员的继承链以查找这些特性</param>
        /// <returns>应用于此成员的自定义特性</returns>
        public static IEnumerable<Attribute> GetCustomAttributes(this MemberInfo memberInfo, bool inherit)
        {
            return memberInfo.GetCustomAttributes(inherit).Select(t => { return (Attribute)t; }).ToArray();
        }

        /// <summary>
        /// 在派生类中重写时，返回由 System.Type 标识的自定义特性的数组
        /// 异常:
        /// T:System.TypeLoadException:无法加载自定义特性类型。
        /// T:System.ArgumentNullException:如果 attributeType 为 null。
        /// T:System.InvalidOperationException:该成员属于加载到仅反射上下文的类型。请参见How to: Load Assemblies into the Reflection-Only Context。
        /// </summary>
        /// <param name="memberInfo">目标成员</param>
        /// <param name="attributeType">要搜索的特性类型。只返回可分配给此类型的特性</param>
        /// <param name="inherit">指定是否搜索该成员的继承链以查找这些特性</param>
        /// <returns>应用于此成员的自定义特性</returns>
        public static Attribute GetCustomAttribute(this MemberInfo memberInfo, Type attributeType, bool inherit)
        {
            object[] attriArr = memberInfo.GetCustomAttributes(attributeType, inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (Attribute)attriArr[0];
        }

        /// <summary>
        /// 获取字段指定类型特性,存在指定类型特性则返回特性对象,否则返回null
        /// </summary>
        /// <typeparam name="T">指定类型特性</typeparam>
        /// <param name="fieldInfo">FieldInfo</param>
        /// <param name="inherit">指示是否查找继承链</param>
        /// <returns>结果</returns>
        public static T GetCustomAttribute<T>(this FieldInfo fieldInfo, bool inherit) where T : Attribute
        {
            object[] attriArr = fieldInfo.GetCustomAttributes(typeof(T), inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (T)attriArr[0];
        }

        /// <summary>
        /// 获取属性指定类型特性,存在指定类型特性则返回特性对象,否则返回null
        /// </summary>
        /// <typeparam name="T">指定类型特性</typeparam>
        /// <param name="propertyInfo">FieldInfo</param>
        /// <param name="inherit">指示是否查找继承链</param>
        /// <returns>结果</returns>
        public static T GetCustomAttribute<T>(this PropertyInfo propertyInfo, bool inherit) where T : Attribute
        {
            object[] attriArr = propertyInfo.GetCustomAttributes(typeof(T), inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (T)attriArr[0];
        }

        /// <summary>
        /// 获取参数指定类型特性,存在指定类型特性则返回特性对象,否则返回null
        /// </summary>
        /// <typeparam name="T">指定类型特性</typeparam>
        /// <param name="parameter">FieldInfo</param>
        /// <param name="inherit">指示是否查找继承链</param>
        /// <returns>结果</returns>
        public static T GetCustomAttribute<T>(this ParameterInfo parameter, bool inherit) where T : Attribute
        {
            object[] attriArr = parameter.GetCustomAttributes(typeof(T), inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (T)attriArr[0];
        }

        /// <summary>
        /// 获取指定类型特性,存在指定类型特性则返回特性对象,否则返回null
        /// </summary>
        /// <typeparam name="T">特性类型</typeparam>
        /// <param name="type">指定类型</param>
        /// <param name="inherit">指示是否查找继承链</param>
        /// <returns>结果</returns>
        public static T GetCustomAttribute<T>(Type type, bool inherit) where T : Attribute
        {
            object[] attriArr = type.GetCustomAttributes(typeof(T), inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (T)attriArr[0];
        }

        /// <summary>
        /// 获取指定类型特性,存在指定类型特性则返回特性对象,否则返回null
        /// </summary>
        /// <param name="type">指定类型</param>
        /// <param name="attributeType">特性类型</param>
        /// <param name="inherit">指示是否查找继承链</param>
        /// <returns>结果</returns>
        public static Attribute GetCustomAttribute(Type type, Type attributeType, bool inherit)
        {
            object[] attriArr = type.GetCustomAttributes(attributeType, inherit);
            if (attriArr == null || attriArr.Length == 0)
            {
                return null;
            }

            return (Attribute)attriArr[0];
        }
#endif

        /// <summary>
        /// 检查类型是否有无参构造函数[true:有无参构造函数;false:没有无参构造函数]
        /// </summary>
        /// <param name="type">被检查类型</param>
        /// <returns>true:有无参构造函数;false:没有无参构造函数</returns>
        public static bool TypeHasNoParaConstructors(Type type)
        {
            return type.IsClass &&
               !type.IsAbstract &&
               type.GetConstructors().Any(t => { return t.GetParameters().Length == 0; });
        }





        #region GetTypeName
        /// <summary>
        /// 获取 System.Type.AssemblyQualifiedName 的程序集限定名，其中包括从中加载 System.Type.AssemblyQualifiedName 的程序集的名称
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <returns>System.Type.AssemblyQualifiedName</returns>
        public static string GetTypeName<T>()
        {
            return GetTypeName(typeof(T));
        }

        /// <summary>
        /// 获取 System.Type.AssemblyQualifiedName 的程序集限定名，其中包括从中加载 System.Type.AssemblyQualifiedName 的程序集的名称
        /// </summary>
        /// <param name="obj">获取类型的名称的对象</param>
        /// <returns>System.Type.AssemblyQualifiedName</returns>
        public static string GetTypeName(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            return GetTypeName(obj.GetType());
        }

        /// <summary>
        /// 获取 System.Type.AssemblyQualifiedName 的程序集限定名，其中包括从中加载 System.Type.AssemblyQualifiedName 的程序集的名称
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>System.Type.AssemblyQualifiedName</returns>
        public static string GetTypeName(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return type.AssemblyQualifiedName;
        }
        #endregion


        /// <summary>
        /// 获得类型默认值
        /// </summary>
        /// <param name="targetType">要获取默认值的目标类型</param>
        /// <returns>类型默认值</returns>
        public static object GetTypeDefaultValue(Type targetType)
        {
            object defaultValue;
            if (targetType.IsValueType)
            {
                defaultValue = Activator.CreateInstance(targetType);
            }
            else
            {
                defaultValue = null;
            }

            return defaultValue;
        }


        #region sizeof

        /// <summary>
        /// 计算类型大小
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <returns>类型大小</returns>
        public static int SizeOf<T>()
             where T : struct, IConvertible, IFormattable, IComparable<T>, IEquatable<T>
        {
            return SizeOf(Type.GetTypeCode(typeof(T)));
        }

        /// <summary>
        /// 计算类型大小
        /// </summary>
        /// <param name="type">数据类型</param>
        /// <returns>类型大小</returns>
        /// <exception cref="ArgumentNullException">数据类型为null异常</exception>
        /// <exception cref="ArgumentException">数据类型为非值类型异常</exception>
        public static int SizeOf(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!type.IsValueType)
            {
                throw new ArgumentException($"计算大小的类型必须为值类型,类型\"{type.FullName}\"不支持");
            }

            return SizeOf(Type.GetTypeCode(type));
        }

        /// <summary>
        /// 计算类型大小
        /// </summary>
        /// <param name="typeCode">数据类型Code</param>
        /// <returns>类型大小</returns>
        public static int SizeOf(TypeCode typeCode)
        {
            switch (typeCode)
            {
                case TypeCode.Boolean:
                case TypeCode.Char:
                case TypeCode.Byte:
                case TypeCode.SByte:
                    return 1;
                case TypeCode.Int16:
                case TypeCode.UInt16:
                    return 2;
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Single:
                    return 4;
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Double:
                    return 8;
                case TypeCode.Decimal:
                    return 16;
                default:
                    throw new NotSupportedException($"计算大小的类型必须为值类型,类型Code\"{typeCode.ToString()}\"不支持");
            }


            //int size;
            //switch (typeCode)
            //{
            //    case TypeCode.Boolean:
            //        size = sizeof(Boolean);
            //        break;
            //    case TypeCode.Char:
            //        size = sizeof(Char);
            //        break;
            //    case TypeCode.Byte:
            //        size = sizeof(Byte);
            //        break;
            //    case TypeCode.SByte:
            //        size = sizeof(SByte);
            //        break;
            //    case TypeCode.Int16:
            //        size = sizeof(Int16);
            //        break;
            //    case TypeCode.UInt16:
            //        size = sizeof(UInt16);
            //        break;
            //    case TypeCode.Int32:
            //        size = sizeof(Int32);
            //        break;
            //    case TypeCode.UInt32:
            //        size = sizeof(UInt32);
            //        break;
            //    case TypeCode.Single:
            //        size = sizeof(Single);
            //        break;
            //    case TypeCode.Int64:
            //        size = sizeof(Int64);
            //        break;
            //    case TypeCode.UInt64:
            //        size = sizeof(UInt64);
            //        break;
            //    case TypeCode.Double:
            //        size = sizeof(Double);
            //        break;
            //    case TypeCode.Decimal:
            //        size = sizeof(Decimal);
            //        break;
            //    default:
            //        throw new NotSupportedException($"计算大小的类型必须为值类型,类型Code\"{typeCode.ToString()}\"不支持");
            //}

            //return size;
        }

        #endregion

    }

}
